#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QStackedWidget>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusInterface>
#include <QObject>
#include <QMap>
#include <QThread>
#include <QProcess>
#include <QMetaType>

#include "connectionservice.h"
#include "wifimanager.h"
#include "ftpmanager.h"
#include "filesyncmanage.h"
#include "usbmanage.h"
#include "devicemanage.h"
#include "classificationlist/mobilefilesecondwidget.h"
#include "classificationlist/mobilefilewidget.h"
#include "initconnectwin/initconnectwin.h"
#include "storagelist/mobilestoragewidget.h"
#include "storagelist/copytocomputerbuttonwidget.h"
#include "view/titlebar.h"
#include "view/connectedtitlewin.h"
#include "view/mobilefilelist.h"
#include "view/transmissiondialog.h"
#include "loadanimation.h"
#include "view/connectdialog.h"
#include "kaboutdialog.h"

#define KYLIN_MOBILE_ASSISTANT_DBUS_SERVICE "com.kylin.mobile_assistant"
#define KYLIN_MOBILE_ASSISTANT_DBUS_PATH "/com/kylin/mobile_assistant"
#define KYLIN_MOBILE_ASSISTANT_DBUS_INTERFACE "com.kylin.mobile_assistant"

const QString MOUNT_DIR = "/var/lib/mult";

using namespace KMobileProjection;
class MainWindow : public QMainWindow
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", KYLIN_MOBILE_ASSISTANT_DBUS_INTERFACE)
public:
    static MainWindow *getInstance();

    // 提供给dbus的接口
    // 获取当前是否有设备连接
    QString getDeviceName();
    // 获取当前目录文件列表
    void list(QString path);
    // 下载
    void downFile(const QList<FileInfo> &fileList, QString downloadPath);

protected:
    void closeEvent(QCloseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;

private:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    enum WIN_FLAGE { WIN_INIT_MAIN, WIN_RETURN_MAIN, WIN_INIT_QQWECHAT };
    enum FILE_TYPE {
        QQPICTURE,
        QQVIDEO,
        QQMUSIC,
        QQDOCUMENT,
        WECHATPICTURE,
        WECHATVIDEO,
        WECHATMUSIC,
        WECHATDOCUMENT,
        PICTURE,
        VIDEO,
        MUSIC,
        DOCUMENT,
        WECHAT,
        QQ,
        HOMEPAGE
    };

    void initService();
    void initGsetting();
    void initDbus();
    void disconnectScreen();
    void disconnectFtp();
    void stopAllDown();
    void disconnectService();
    void deleterService();
    bool mountFtp(QString url, QString userName, QString pwd, QString path);
    bool umountFtp(QString path);
    void clearConnectionInfo();

    void changeLightTheme();
    void changeDarkTheme();
    void initUI();
    void connectUI();
    void updateResult();
    void setScreenState(bool isScreenShow);
    void initTransmissionDialog();
    void isExistWidget();
    void initMobileListWidget(int tag);
    void connectMobileFileWidget();
    void setToolTipWin(QString msg);
    void uploadLocalListPath(QString itemName, const QStringList &localPath);
    void moveCopyToComputerWidget();
    void deleterUI();
    void startLoadUI(int y);
    void stopLoadUI();
    void updateCurrentPath(int text);
    int getScreenIndex();

private:
    ConnectionService *m_connectionService = nullptr; // 连接服务
    FtpManager *m_ftpClient = nullptr;                // ftp服务器
    DeviceManage *m_deviceManage = nullptr;           // 连接手机投屏
    UsbManage *m_usbManage = nullptr;                 // 监听USB插拔信号
    WifiManager *m_wifiManager = nullptr;             // 监听wifi信号
    QThread *m_wifiManagerThread = nullptr;           // 另起线程，监听wifi信号
    FileSyncManage *m_fileSyncManage = nullptr;       // 文件同步
    QDBusInterface *m_dbusInterface = nullptr;        // 挂载ftpdubs


    bool m_isConnect = false;                                      // 当前是否连接
    QString m_currentPath = "";                                    // 手机存储当前路径
    int m_downFileType = FileSyncManage::LIST_TYPE::TYPE_FILE_NUM; // 当前下载类型
    QUrl m_url;                                                    // 服务器地址
    ConnectionService::ConnectionInfo m_connectInfo;               // 当前连接设备信息
    bool m_isDbusOperation = false;                                //当前操作是否为dbus调用
    bool m_isMountFtp = false;                                     //是否挂载ftp
    bool m_isServerConnect = false;                                // 服务是否连接
    QString m_uploadPath = "";                                     // 当前为上传

    Titlebar *m_titlebar = nullptr;                             // 标题栏
    InitConnectWin *m_initConnectWin = nullptr;                 // 连接界面
    ConnectedTitleWin *m_connectedTitleWin = nullptr;           // 连接信息栏
    MobileFileList *m_mobileFileList = nullptr;                 // 文件分类界面
    QStackedWidget *m_stackedWin = nullptr;                     // 连接与未连接的堆栈窗口
    QStackedWidget *m_stackedMobileFileListWin = nullptr;       // 分类界面与文件信息界面的堆栈窗口
    kdk::KAboutDialog *m_aboutWindow = nullptr;                 //关于窗口
    MobileFileWidget *m_listItemWidget = nullptr;               // 文件信息展示界面
    MobileStorageWidget *m_mobileStorageWidget = nullptr;       // 手机存储界面
    TransmissionDialog *m_transmissionDialog = nullptr;         // 传输进度窗口
    MobileFileSecondWidget *m_mobileFileSecondWidget = nullptr; // QQ微信分类界面
    ConnectDialog *m_connectDialog = nullptr;                   // 连接加载界面
    LoadAnimation *m_loadAnimation = nullptr;                   // 打开/更新加载界面
    QTimer *m_timer = nullptr;                                  // 连接加载定时器，连接超时处理
    CopyToComputerButtonWidget *m_copyToComputerButtonWidget = nullptr; // 复制按钮

    int m_index = 2;                                                // 堆栈窗口索引
    int m_updateWin = WIN_FLAGE::WIN_INIT_MAIN;                     // 当前切换界面
    QListView::ViewMode m_classificationMode = QListView::IconMode; // 分类列表当前视图模式
    QListView::ViewMode m_storageMode = QListView::IconMode;        // 手机存储当前视图模式
    QString m_tipMesseage = "";                                     // 提示消息

Q_SIGNALS:
    // 提供dbus服务
    void sigConnected(QString);
    void sigDisconnect();
    void sigFileInfoList(const QMap<QString, FileInfo> &);

    void sigClearSearchLine();
    void sigSearchUpdate();
public Q_SLOTS:
    // 外部
    void setLocalPathList(const QStringList &list);

    // 服务
    void slotVolumeConnected(QString volumeName);
    void slotVolumeDisconnected(QString volumeName);
    void slotWifiConnected(QString wifiIP);
    void slotWifiDisConnected();
    void slotConnect(const ConnectionService::ConnectionInfo &connectInfo);
    void slotDisconnect();
    void slotNotFountApk();
    void slotConnectFailed();
    void slotFtpConnected();
    void slotCurrentDirectoryList(const QMap<QString, FileInfo> &filelist);
    void slotTransferProgress(qint64 readBytes, qint64 totalBytes);
    void slotDownFileFinish(QString flag, QString filepath);
    void slotDownAllFileFinish(QString flag);
    void slotDownTempFileFinish(QString filePath);
    void slotDownFileFail(QString fileName, QString flag);
    void slotDownFileFail(QString fileName);
    void slotMountError(bool isMount, QString error);

    // UI
    void slotAboutWinShow();
    void onRecvSearchResult(const QMap<QString, FileInfo> &searchResult);
    void slotUSBConnectOnClicked();
    void slotSetMobileWindow();
    void slotItemButtonClicked(int text);
    void slotCdMobileStorageDirectory(QString path);
    void slotDownThumb();
    void slotMenuFilePath(QString filePath);
    void slotSetLastWidget();
    void slotShowMobileFileListWidget();
    void slotLocalPathList(const QStringList &localPathList);
    void slotShowMobileFileSecondWidget(int indexPath);
    void slotShowMobileStorage();
    void slotChooseFilePath(QString chooseFilePath, const QList<FileInfo> &selectFileNameList);
    void slotAbortTransmission();
    void slotShowCopyButtonWidget();
    void slotHideCopyToComputerWidget();
    void slotSelectDataList(const QList<FileInfo> &fileList);
    void slotSearchInitFail();
    void slotSearchUpdateClicked();
};

#endif // MAINWINDOW_H
